package util;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBUtil {

	private static String DRIVER;
	private static String URL;
	private static String USERNAME;
	private static String PASSWORD;
	private static Connection conn;
	Statement stmt;
	ResultSet rs;

	public static  void initParam(String paramFile) throws Exception {
		Properties props = new Properties();
		String fullFileName = DBUtil.class.getResource(paramFile).getFile();
		props.load(new FileInputStream(fullFileName));
		DRIVER = props.getProperty("DRIVER");
		URL = props.getProperty("URL");
		USERNAME = props.getProperty("USERNAME");
		PASSWORD = props.getProperty("PASSWORD");
	}
	public static   Connection getConnection(){
		try {
			initParam("mysql.ini");
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL , USERNAME , PASSWORD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(SQLException e){
			e.printStackTrace();
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	
	public void closeConn(Connection conn){
		if(conn != null){
			try{
				conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	
}
